//
//  UIFont+appFont.m
//  SlockApp
//
//  Created by 凯朱 on 2024/7/30.
//

#import "UIFont+appFont.h"
#import <objc/runtime.h>
#define CustomRegularFontName @"SFUIDisplay-Regular"
#define CustomBoldFontName @"SFUIDisplay-Bold"
#define CustomMediumFontName @"SFUIDisplay-Medium"
#define CustomLightFontName @"SFUIDisplay-Light"
#define CustomSemiboldFontName @"SFUIDisplay-Semibold"

typedef id (*_IMP)(id,SEL,...);  // 有返回值
typedef id (*_VIMP)(id,SEL,...); // 无返回值

@implementation UIFont (appFont)
+ (void)load {
    
    //  线程安全
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        Class class = [self class];
        
        //  系统systemFontOfSize方法
        Method systemMethod = class_getClassMethod(class, @selector(systemFontOfSize:));
        //  自定义systemFontOfSize方法
        Method swizzMethod = class_getClassMethod(class, @selector(my_systemFontOfSize:));
        //  交换方法
        method_exchangeImplementations(systemMethod, swizzMethod);

        //  boldSystemFontOfSize
        Method systemMethod1 = class_getClassMethod(class, @selector(boldSystemFontOfSize:));
        Method swizzMethod1 = class_getClassMethod(class, @selector(my_boldSystemFontOfSize:));
        method_exchangeImplementations(systemMethod1, swizzMethod1);
       
        //  meidumSystemFontOfSize
        Method systemMethod2 = class_getClassMethod(class, @selector(systemFontOfSize:weight:));
        Method swizzMethod2 = class_getClassMethod(class, @selector(my_mediumSystemFontOfSize:weight:));
        method_exchangeImplementations(systemMethod2, swizzMethod2);
       
    });
}


/// 自定义正常字体
/// @param size siz
+ (UIFont *)my_systemFontOfSize:(CGFloat)size {
    UIFont *font = [UIFont fontWithName:CustomRegularFontName size:size];
    if (font) {
        return font;
    }
    //  字体缺失时防奔溃处理
    return [UIFont systemFontOfSize:size];
}

/// 自定义加粗字体
/// @param size siz
+ (UIFont *)my_boldSystemFontOfSize:(CGFloat)size {

    UIFont *font = [UIFont fontWithName:CustomBoldFontName size:size];
    if (font) {
        return font;
    }
    return [UIFont boldSystemFontOfSize:size];
}


+ (UIFont *)my_italicSystemFontOfSize:(CGFloat)size {

    UIFont *font = [UIFont fontWithName:CustomRegularFontName size:size];
    if (font) {
        return font;
    }
    return [UIFont italicSystemFontOfSize:size];
}

+ (UIFont *)my_mediumSystemFontOfSize:(CGFloat)size weight:(UIFontWeight)weight{

    UIFont *font = [UIFont fontWithName:CustomMediumFontName size:size];
    if (font) {
        return font;
    }
    return [UIFont systemFontOfSize:size weight:weight];
    
    
}



@end


